package com.xlhj.boot.service.impl;

import com.xlhj.boot.service.TtlMsgProducerService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author: liucaijing
 * @description: 延迟队列(消息延迟)
 * @date: 2021/10/20 9:02
 */
@Service
public class TtlMsgProducerServiceImpl implements TtlMsgProducerService {
    private static final String TTL_MSG_QUEUE_EXCHANGE = "ttl-msg-queue-exchange";
    private static final String TTL_MSG_QUEUE_ROUT_KEY = "ttl_msg_queue-routing-key";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void sendMessage(String message, String ttlTime) {
        String uuid = UUID.randomUUID().toString();
        CorrelationData correlationData = new CorrelationData(uuid);
        rabbitTemplate.convertAndSend(TTL_MSG_QUEUE_EXCHANGE, TTL_MSG_QUEUE_ROUT_KEY, message, processor -> {
            processor.getMessageProperties().setExpiration(ttlTime);
            processor.getMessageProperties().getHeaders().put("userId", "10000");
            return processor;
        }, correlationData);
    }

}
